<?php

/*
 * 用户注册页面
 */

include 'common.php';

if (Utils::submitCheck('registersubmit')) {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    $password2 = trim($_POST['password2']);
    $email = trim($_POST['email']);
    $compny_name = trim($_POST['company_name']);
    $compny_website = trim($_POST['company_website']);

    if (empty($username) || empty($password)) {
        Utils::showMessage('username_password_not_exists');
    } elseif ($password != $password2) {
        Utils::showMessage('too_password_is_diff');
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        Utils::showMessage('email_error');
    } elseif (empty($compny_name) || empty($compny_website)) {
        Utils::showMessage('company_info_must_full');
    } elseif (!filter_var($compny_website, FILTER_VALIDATE_URL)) {
        Utils::showMessage('url_error');
    } elseif (Company::isHave($compny_name, $compny_website)) {
        Utils::showMessage('company_is_exists');
    } elseif (User::isHave($username)) {
        Utils::showMessage('user_is_exists');
    } else {
        $cid = Company::addCompany($compny_name, $compny_website, '', '');
        $uid = User::addUser($username, $password, $email, 0, $cid, '', 1);
        if ($uid) {
            $_SGLOBAL['member'] = User::getUser($uid);
            $encode = LoginAuth::encode($_SGLOBAL['member']['uid'] . "\t" . $_SGLOBAL['member']['password']);
            Cookie::_set('auth', $encode);
            header("Location: index.php");
        }
    }
}


$_SGLOBAL['view']->display('register');
?>
